exports.__esModule = true;

var _jimp = require('jimp');

var _jimp2 = _interopRequireDefault(_jimp);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

const unlink = think.promisify(_fs2.default.unlink, _fs2.default);

exports.default = class extends think.Service {
    constructor() {
        super();
        const db = this.db;
        this.userModel = db['sys_user'];
        this.orgModel = db['sys_org'];
        this.roleUserModel = db['sys_role_user'];
        this.menuModel = db['sys_menu'];
        this.roleMenuModel = db['sys_role_menu'];
        this.roleModel = db['sys_role'];
        this.serverIP = 'http://' + think.config('staticIp') + ':' + think.config('proxyPort');

        this.userModel.belongsToMany(this.roleModel, {
            through: this.roleUserModel, foreignKey: 'userid', otherKey: 'roleid'
        });
    }
    /**
     * 移动端登录
     * @data
     * data.phone --电话号码
     * data.password --密码
     * 
     * @return
     *result --用户信息 Object
     */
    isLogin(data1, data2) {
        var _this = this;

        return _asyncToGenerator(function* () {
            const upfileds = data2;
            // data1.password = think.irreversibleEncrypt(data1.username,data1.password);//加密
            try {
                if (_this.isPoneAvailable(data1.username)) {
                    const appUser = yield _this.userModel.findOne({ where: { phone: data1.username }, attributes: ['username'] });
                    if (appUser) {
                        data1.username = appUser.username;
                    }
                }
                data1.password = think.irreversibleEncrypt(data1.username, data1.password); //加密
                const UserInfo = yield _this.userModel.findOne({
                    where: data1,
                    // attributes:['id','username','password','realname','job_no','org_id','status','pic_path','gender','phone','email','user_type']
                    attributes: { exclude: ['birth', 'last_login_time', 'login_ip', 'create_date', 'create_by', 'update_date', 'update_by'] }
                });
                if (UserInfo) {
                    if (UserInfo.login_flag === 0) {
                        return { msg: '该账号已被锁定，请联系管理员' };
                    } else if (UserInfo.del_flag === 1) {
                        return { msg: "该账号已被删除，请联系管理员恢复" };
                    } else {
                        let role = yield _this.roleUserModel.findAll({ where: { userid: UserInfo.id }, attributes: ['roleid'] });
                        if (role.length === 0) {
                            return { msg: '该账号未分配权限，请联系管理员' };
                        } else {
                            yield _this.userModel.update(upfileds, { where: data1 });
                            role = role.map(function (r) {
                                return r.roleid;
                            });
                            UserInfo.dataValues.org_name = "无";
                            if (UserInfo.pic_path) {
                                UserInfo.dataValues.pic_path = _this.serverIP + '/' + UserInfo.pic_path;
                            } else {
                                UserInfo.dataValues.pic_path = _this.serverIP + '/static/upload/images/default.png';
                            }
                            const org = yield _this.orgModel.findOne({ attributes: ['org_name'], where: { id: UserInfo.org_id } });
                            if (org) {
                                UserInfo.dataValues.org_name = org.org_name;
                            }
                            const menuList = yield _this.getMenu(role);
                            if (!menuList.error) {
                                UserInfo.dataValues.menu = menuList;

                                //把用户所有登陆信息缓存
                                const nameSpace = think.config('nameSpace');

                                //移动端用户登录不缓存至redis
                                // think.Redis.hset(nameSpace.LOGIN_USER, UserInfo.id, JSON.stringify(UserInfo));
                                const tokenContent = {
                                    userId: UserInfo.id,
                                    realName: UserInfo.realname,
                                    orgId: UserInfo.org_id,
                                    roleList: role
                                };
                                //获取token
                                const token = _this.BaseService.getTokenForApp(tokenContent);

                                return { UserInfo, token };
                            } else {
                                return { msg: '获取权限配置失败' };
                            }
                        }
                    }
                } else {
                    return { msg: "用户名或密码输入错误" };
                }
            } catch (err) {
                return _this.exceptionService.handleError(err);
            }
        })();
    }
    //判断是否为手机号  
    isPoneAvailable(pone) {
        var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        if (!myreg.test(pone)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取角色绑定的所有菜单列表
     * roleList --Array 角色id数组
     */
    getMenu(roleList) {
        var _this2 = this;

        return _asyncToGenerator(function* () {
            const Op = _this2.db.Sequelize.Op;
            let list = [];
            try {
                let menuIds = yield _this2.roleMenuModel.findAll({
                    where: {
                        roleid: {
                            [Op.in]: roleList
                        }
                    },
                    attributes: ['menuid']
                });
                if (menuIds.length > 0) {
                    menuIds = menuIds.map(function (item) {
                        return item.menuid;
                    });
                    menuIds = ArrayUtils.unique(menuIds); //去重
                    let menus = yield _this2.menuModel.findAll({ //只要移动端菜单
                        where: {
                            id: {
                                [Op.in]: menuIds
                            },
                            menu_source: 2
                        },
                        attributes: ['id']
                    });
                    if (menus.length > 0) {
                        list = menus.map(function (item2) {
                            return item2.id;
                        });
                    }
                }
                return list;
            } catch (err) {
                return _this2.exceptionService.handleError(err);
            }
        })();
    }

    /**
     * 
     * @param {*修改用户信息} 
     * data 更新内容
     * userid 用户id
     */
    saveUserInfo(data, userid) {
        var _this3 = this;

        return _asyncToGenerator(function* () {
            let upfileds = data;
            upfileds.update_by = userid;
            upfileds.update_date = new Date();
            try {
                return yield _this3.userModel.update(upfileds, { where: { id: userid } });
            } catch (err) {
                return _this3.exceptionService.handleError(err);
            }
        })();
    }
    /**
     * 修改密码
     * @data
     * data.UserID --用户id
     * data.OldPsd -- 旧密码
     * data.NewPsd -- 新密码
     */
    changePsd(data) {
        var _this4 = this;

        return _asyncToGenerator(function* () {
            const where = { id: data.UserID };
            try {
                const user = yield _this4.userModel.findOne({ where: where, attributes: ['username', 'password'] });
                if (user) {
                    const oldPass = think.irreversibleEncrypt(user.username, data.OldPsd); //加密
                    if (oldPass === user.password) {
                        const upfileds = {
                            password: think.irreversibleEncrypt(user.username, data.NewPsd),
                            update_by: data.UserID,
                            update_date: new Date()
                        };
                        return yield _this4.userModel.update(upfileds, { where: where });
                    } else {
                        return { msg: '旧密码输入错误' };
                    }
                } else {
                    return { msg: '查询不到当前用户信息' };
                }
            } catch (err) {
                return _this4.exceptionService.handleError(err);
            }
        })();
    }
    /**
     * 获取用户详细信息
     */
    getUserInfo(data) {
        var _this5 = this;

        return _asyncToGenerator(function* () {
            const where = { id: data.UserID };
            try {
                let user = yield _this5.userModel.findOne({
                    where: where,
                    attributes: { exclude: ['create_by', 'create_date', 'update_by', 'update_date', 'is_login', 'del_flag', 'login_ip'] },
                    include: [{ model: _this5.roleModel, attributes: ['rolename'] }]
                });
                if (user) {
                    if (user.org_id) {
                        const org = yield _this5.orgModel.findOne({ where: { id: user.org_id }, attributes: ['org_name'] });
                        if (org) {
                            user.dataValues.org_name = org.org_name;
                        } else {
                            user.dataValues.org_name = '无';
                        }
                    } else {
                        user.dataValues.org_name = '无';
                    }

                    if (user.pic_path) {
                        user.dataValues.pic_path = _this5.serverIP + '/' + user.pic_path;
                    } else {
                        user.dataValues.pic_path = _this5.serverIP + '/static/upload/images/default.png';
                    }
                    return user;
                } else {
                    return { msg: '查询不到当前用户信息' };
                }
            } catch (err) {
                return _this5.exceptionService.handleError(err);
            }
        })();
    }

    /**
     * 按真实姓名模糊分页查询
     * @data
     * data.page -- 页码
     * data.pageSize -- 单页条数
     * data.realname -- 真实姓名连续关键字
     */
    getUserListByRname(data) {
        var _this6 = this;

        return _asyncToGenerator(function* () {
            data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
            const Op = _this6.db.Sequelize.Op;
            let where = {
                realname: {
                    [Op.like]: '%' + data.realname + '%'
                }
            };
            if (data.realname === '') {
                //空值返回空数组
                where.realname = '';
            }
            try {
                let list = yield _this6.userModel.findAndCountAll({
                    where: where,
                    attributes: { exclude: ['create_by', 'create_date', 'update_by', 'update_date', 'is_login', 'del_flag', 'login_ip'] },
                    limit: parseInt(data.pageSize),
                    offset: parseInt(data.start)
                });
                if (list.rows && list.rows.length > 0) {
                    for (var i in list.rows) {
                        if (list.rows[i].pic_path) {
                            list.rows[i].dataValues.pic_path = _this6.serverIP + '/' + list.rows[i].pic_path;
                        } else {
                            list.rows[i].dataValues.pic_path = _this6.serverIP + '/static/upload/images/default.png';
                        }
                    }
                }
                return list;
            } catch (err) {
                return _this6.exceptionService.handleError(err);
            }
        })();
    }
    /**
     * 统计用户各类待办工作count,待核实，未读通知公告，待审批
     */
    getUserTaskCount(data) {
        var _this7 = this;

        return _asyncToGenerator(function* () {
            let roleSqlStr = '';

            try {
                let roleList = yield _this7.roleUserModel.findAll({ where: { userid: data.userId }, attributes: ['roleid'] });
                if (roleList.length > 0) {
                    for (var i = 0; i < roleList.length; i++) {
                        roleSqlStr += ` OR b.group LIKE '%"${roleList[i].roleid}"'`;
                    }
                }
                const countSql = `SELECT ( SELECT count(*) FROM cms_event WHERE verify_by = a.id AND is_check = 0 ) AS verifyCount, 
                            ( SELECT count(*) FROM oa_notify_record WHERE user_id = a.id AND read_flag = 0 ) AS notifyCount, 
                            ( SELECT count(*) count FROM ( SELECT count(*) count FROM bpmn_hi_actinst b
                                LEFT JOIN bpmn_hi_procinst c ON b.proc_inst_id = c.id 
                                LEFT JOIN bpmn_re_procdef d ON d.id = c.proc_def_id 
                                WHERE act_status = 0 AND ( b.assignee LIKE '%"${data.userId}"%' ${roleSqlStr}) AND c.id != '' AND c.status = 2 GROUP BY c.id ) AS c ) AS reviewCount 
                            FROM sys_user a WHERE a.id = "${data.userId}";`;
                const userTaskCount = yield _this7.db.sequelize.query(countSql, { type: _this7.db.sequelize.QueryTypes.SELECT });
                if (userTaskCount.length > 0) {
                    return userTaskCount[0];
                } else {
                    return { error: '获取用户待办工作统计信息失败' };
                }
            } catch (err) {
                return _this7.exceptionService.handleError(err);
            }
        })();
    }
};